<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">权限</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>权限列表</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="roleDialogVisible = !roleDialogVisible">添加角色</el-button>
                </el-col>
            </el-row>

            <el-table :data="rolelist" stripe border>
                <el-table-column type="expand">
                    <template slot-scope="scope" >
                        <el-row :class="['bdbuttom','el_row_padding','center', index === 0 ? 'bdtop' : '']" v-for="(item1, index) in scope.row.children" :key="item1.id">
                            <el-col :span="5">
                                <el-tag closable @close="removePowerById(item1.id,scope.row)">{{item1.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <el-col :span="19">
                                <el-row
                                        :class="['bdtop','el_row_padding_top','center', index === 0 ? 'bdtop_top' : '',index > 0 ? 'el_row_padding' : '']"

                                        v-for="(item2, index) in item1.children" :key="item2.id">
                                    <el-col :span="5">
                                        <el-tag closable @close="removePowerById(item2.id,scope.row)" type="success">{{item2.authName}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>

                                    <el-col :span="19" >
                                        <el-row>
                                            <el-col>
                                                <el-tag closable @close="removePowerById(item3.id,scope.row)" class="" type="warning" v-for="item3 in item2.children" :key="item3.id">{{item3.authName}}</el-tag>
                                            </el-col>
                                        </el-row>

                                    </el-col>

                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <el-table-column  type="index"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作" width="300px">
                    <template slot-scope="scope" >
                        <el-button size="mini" type="primary" icon="el-icon-search">编辑</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-search" @click="removeRolesById(scope.row.id)">删除</el-button>
                        <el-button size="mini" type="warning" icon="el-icon-search" @click="showSetRightDialog(scope.row)">分配权限</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>


        <el-dialog
                :close-on-click-modal="false"
                @close="roleDialogClosed"
                resetFields
                title="提示"
                :visible.sync="roleDialogVisible"
                width="40%"
                center>
            <el-form
                    :model="addRoles"
                    :rules="roleFormRules"
                    ref="roleFormRef"
                    label-width="80px"
                    class="demo-ruleForm">

                <el-form-item label="角色名称" prop="roleName ">
                    <el-input v-model="addRoles.roleName"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc ">
                    <el-input v-model="addRoles.roleDesc"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                    <el-button @click="roleDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="roleUser">确 定</el-button>
                </span>
        </el-dialog>

        <el-dialog
                @close="setRightDialogClosed"
                title="分配权限"
                :visible.sync="setRightDialogVisible"
                width="30%">
            <el-tree

                    node-key
                    show-checkbox
                    default-expand-all
                    :default-checked-keys="defKeys"
                    node-key="id"
                    ref="treeRef"
                    :data="rightslist"
                    :props="treeProps" ></el-tree>
            <span slot="footer" class="dialog-footer">
                <el-button @click="setRightDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="alloRights">确 定</el-button>
            </span>
        </el-dialog>


    </div>
</template>

<script>
    export default {
        name: "Roles",
        data(){
            return {
                treeProps:{
                    children: 'children',
                    label: 'authName'
                },
                roleId:'',
                rightslist :[],
                rolelist:[],
                defKeys:[],
                roleFormRules:{
                    roleName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    roleDesc: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],

                    roleId: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
                    ],

                },
                roleDialogVisible: false,
                setRightDialogVisible:false,

                addRoles:{
                    roleId:'',
                    roleName:'',
                    roleDesc:''
                }
            }
        },
        created() {
            this.getRolesList()
            console.log(this.rolelist)
        },
        methods:{
            async getRolesList(){
               const {data:res} = await this.$http.get('roles')
                console.log(res)
                if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.rolelist = res.data
            },
            // 表达重置
            roleDialogClosed(){
                this.$refs.roleFormRef.resetFields()
            },
            roleUser(){
                this.$refs.roleFormRef.validate(async valid => {
                    if(!valid) return
                    const {data:res} = await this.$http.post('roles',this.addRoles )
                    console.log(res)
                    this.roleDialogVisible = false
                    this.getRolesList()
                })
            },
            removeRolesById(id){
                console.log(id)

                this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const {data:res} = await this.$http.delete('roles/' + id)
                    if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getRolesList()
                }).catch(() => {
                });
            },
            removePowerById(id3,id1){
                this.$confirm('此操作将永久删除用户权限, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const {data:res} = await this.$http.delete('roles/'+ id1.id +'/rights/' + id3)
                    if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    // this.getRolesList()
                    id1.children = res.data
                }).catch(() => {
                });
            },
            async showSetRightDialog(node) {
                const {data:res} = await this.$http.get('rights/tree')
                console.log(res)
                console.log(node)
                this.roleId = node.id

                if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.$message.success(res.meta.msg)
                this.rightslist = res.data
                this.getLeafKeys(node,this.defKeys)

                this.setRightDialogVisible = true


            },
            getLeafKeys(node, arr){
                if(!node.children){
                    return arr.push(node.id)
                }
                node.children.forEach(item => {
                    this.getLeafKeys(item,arr)
                })
            },
            setRightDialogClosed(){
                this.defKeys = []
            },
            async alloRights(){
                const tree = [
                    ...this.$refs.treeRef.getHalfCheckedKeys(),
                    ...this.$refs.treeRef.getCheckedKeys()
                ]
                const iftree = tree.join(',')
                console.log(iftree)

                const {data:res} = await this.$http.post(`roles/${this.roleId}/rights`, {rids: iftree})
                console.log(res)
                this.getRolesList()
                this.setRightDialogVisible = false
            }
        }
    }
</script>

<style scoped>
    .el_row_padding_top {
        padding: 0 0 10px 0;
    }
    .el_row_padding{
        padding: 10px 0;
    }
    .bdtop{
        border-top: 1px solid #eeeeee;
    }
    .bdbuttom{
        border-bottom: 1px solid #eeeeee;
    }
    .bdtop_top{
        border-top: 0px;
    }
    .center{
        display: flex;
        align-items: center;
    }
</style>